package com.example.anan.AAChartCore.ChartsDemo.MainContent.slice;

import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAChartModel;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAChartView;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AAMoveOverEventMessageModel;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartCreator.AASeriesElement;
import com.example.anan.AAChartCore.AAChartCoreLib.AAChartEnum.*;
import com.example.anan.AAChartCore.AAChartCoreLib.AATools.AAGradientColor;
import com.example.anan.AAChartCore.AAChartCoreLib.AATools.AALinearGradientDirection;
import com.example.anan.AAChartCore.ResourceTable;
import com.google.gson.Gson;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;

import java.util.Map;

public class BasicChartAbilitySlice extends AbilitySlice implements
        AAChartView.AAChartViewCallBack {

    private AAChartModel aaChartModel;
    private AAChartView aaChartView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_basic_chart);
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_status_bar_color).getColor();
            getWindow().setStatusBarColor(color);
        } catch (Exception e) {
            e.printStackTrace();
        }
        setUpRadioButtonsAndSwitches();
        setUpAAChartView(intent);
    }

    void setUpAAChartView(Intent intent) {

        aaChartView = (AAChartView) findComponentById(ResourceTable.Id_AAChartView);
        aaChartView.callBack = this;
        aaChartModel = configureAAChartModel(intent);
        aaChartView.aa_drawChartWithChartModel(aaChartModel);

    }

    AAChartModel configureAAChartModel(Intent intent) {
        String chartType = intent.getStringParam("chartType");
        int position = intent.getIntParam("position", 0);

        AASeriesElement element1 = new AASeriesElement()
                .name("Tokyo")
                .data(new Object[]{7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6});
        AASeriesElement element2 = new AASeriesElement()
                .name("NewYork")
                .data(new Object[]{0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5});
        AASeriesElement element3 = new AASeriesElement()
                .name("London")
                .data(new Object[]{0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0});
        AASeriesElement element4 = new AASeriesElement()
                .name("Berlin")
                .data(new Object[]{3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8});

        AAChartModel aaChartModel = new AAChartModel()
                .chartType(AAChartType.Area)
                .title("THE HEAT OF PROGRAMMING LANGUAGE")
                .subtitle("Virtual Data")
                .backgroundColor("#4b2b7f")
                .categories(new String[]{"Java", "Swift", "Python", "Ruby", "PHP", "Go", "C", "C#", "C++"})
                .dataLabelsEnabled(false)
                .yAxisGridLineWidth(0f)
                .touchEventEnabled(true)
                .series(new AASeriesElement[]{element1, element2, element3, element4});


        this.aaChartModel = aaChartModel;
        configureTheStyleForDifferentTypeChart(chartType, position);

        return aaChartModel;
    }

    void configureTheStyleForDifferentTypeChart(String chartType, int position) {
        if ((chartType.equals(AAChartType.Area) || chartType.equals(AAChartType.Line))
                && (position == 4 || position == 5)) {
            configureStepAreaChartAndStepLineChart();
        } else if (chartType.equals(AAChartType.Column) || chartType.equals(AAChartType.Bar)) {
            configureColumnChartAndBarChartStyle();
        } else if (chartType.equals(AAChartType.Area) || chartType.equals(AAChartType.Areaspline)) {
            configureAreaChartAndAreasplineChartStyle(chartType);
        } else if (chartType.equals(AAChartType.Line) || chartType.equals(AAChartType.Spline)) {
            configureLineChartAndSplineChartStyle(chartType);
        }

    }

    private void configureStepAreaChartAndStepLineChart() {
        AASeriesElement element1 = new AASeriesElement()
                .name("Tokyo")
                .step(true)
                .data(new Object[]{149.9, 171.5, 106.4, 129.2, 144.0, 176.0, 135.6, 188.5, 276.4, 214.1, 95.6, 54.4});

        AASeriesElement element2 = new AASeriesElement()
                .name("NewYork")
                .step(true)
                .data(new Object[]{83.6, 78.8, 188.5, 93.4, 106.0, 84.5, 105.0, 104.3, 131.2, 153.5, 226.6, 192.3});

        AASeriesElement element3 = new AASeriesElement()
                .name("London")
                .step(true)
                .data(new Object[]{48.9, 38.8, 19.3, 41.4, 47.0, 28.3, 59.0, 69.6, 52.4, 65.2, 53.3, 72.2});

        aaChartModel.series(new AASeriesElement[]{element1, element2, element3});
    }

    private void configureColumnChartAndBarChartStyle() {
        aaChartModel
                .categories(new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"})
                .legendEnabled(true)
                .colorsTheme(new String[]{"#fe117c", "#ffc069", "#06caf4", "#7dffc0"})
                .animationType(AAChartAnimationType.EaseOutCubic)
                .animationDuration(1200);
    }

    private void configureAreaChartAndAreasplineChartStyle(String chartType) {
        aaChartModel
                .animationType(AAChartAnimationType.EaseOutQuart)
                .legendEnabled(true)
                .markerRadius(5f)
                .markerSymbol(AAChartSymbolType.Circle)
                .markerSymbolStyle(AAChartSymbolStyleType.InnerBlank);

        if (chartType.equals(AAChartType.Areaspline)) {
//            Map gradientColorDic = AAGradientColor.linearGradient(
//                    AALinearGradientDirection.ToBottomRight,
//                    "rgba(138,43,226,1)",
//                    "rgba(30,144,255,1)" //颜色字符串设置支持十六进制类型和 rgba 类型
//            );

            AASeriesElement element1 = new AASeriesElement()
                    .name("Predefined symbol")
//                    .fillColor(gradientColorDic)
                    .data(new Object[]{0.45, 0.43, 0.50, 0.55, 0.58, 0.62, 0.83, 0.39, 0.56, 0.67, 0.50, 0.34, 0.50, 0.67, 0.58, 0.29, 0.46, 0.23, 0.47, 0.46, 0.38, 0.56, 0.48, 0.36});

            AASeriesElement element2 = new AASeriesElement()
                    .name("Image symbol")
                    .data(new Object[]{0.38, 0.31, 0.32, 0.32, 0.64, 0.66, 0.86, 0.47, 0.52, 0.75, 0.52, 0.56, 0.54, 0.60, 0.46, 0.63, 0.54, 0.51, 0.58, 0.64, 0.60, 0.45, 0.36, 0.67});

            AASeriesElement element3 = new AASeriesElement()
                    .name("Base64 symbol (*)")
                    .data(new Object[]{0.46, 0.32, 0.53, 0.58, 0.86, 0.68, 0.85, 0.73, 0.69, 0.71, 0.91, 0.74, 0.60, 0.50, 0.39, 0.67, 0.55, 0.49, 0.65, 0.45, 0.64, 0.47, 0.63, 0.64});

            AASeriesElement element4 = new AASeriesElement()
                    .name("Custom symbol")
                    .data(new Object[]{0.60, 0.51, 0.52, 0.53, 0.64, 0.84, 0.65, 0.68, 0.63, 0.47, 0.72, 0.60, 0.65, 0.74, 0.66, 0.65, 0.71, 0.59, 0.65, 0.77, 0.52, 0.53, 0.58, 0.53});

            aaChartModel
                    .animationType(AAChartAnimationType.EaseFrom)//设置图表渲染动画类型为 EaseFrom
                    .series(new AASeriesElement[]{element1, element2, element3, element4});
        }
    }

    private void configureLineChartAndSplineChartStyle(String chartType) {
        aaChartModel
                .chartType(chartType)
                .markerSymbolStyle(AAChartSymbolStyleType.BorderBlank)//设置折线连接点样式为:边缘白色
                .markerRadius(6f);
        if (chartType.equals(AAChartType.Spline)) {
            AASeriesElement element1 = new AASeriesElement()
                    .name("Tokyo")
                    .lineWidth(7f)
                    .data(new Object[]{50, 320, 230, 370, 230, 400,});

            AASeriesElement element2 = new AASeriesElement()
                    .name("Berlin")
                    .lineWidth(7f)
                    .data(new Object[]{80, 390, 210, 340, 240, 350,});

            AASeriesElement element3 = new AASeriesElement()
                    .name("New York")
                    .lineWidth(7f)
                    .data(new Object[]{100, 370, 180, 280, 260, 300,});

            AASeriesElement element4 = new AASeriesElement()
                    .name("London")
                    .lineWidth(7f)
                    .data(new Object[]{130, 350, 160, 310, 250, 268,});

            aaChartModel
                    .animationType(AAChartAnimationType.SwingFromTo)
                    .series(new AASeriesElement[]{element1, element2, element3, element4});

        }
    }

    private StateElement getButtonElement() {
        Rect bounds = new Rect(0, 0, 50, 50);
        StateElement stateElement = new StateElement();

        ShapeElement shapeElement1 = new ShapeElement(getContext(), ResourceTable.Graphic_shape_rb_c);
        ShapeElement shapeElement2 = new ShapeElement(getContext(), ResourceTable.Graphic_shape_rb_df);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, shapeElement1);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, shapeElement2);

        stateElement.setBounds(bounds);

        return stateElement;
    }

    private StateElement getThumbElement() {
        StateElement stateElement = new StateElement();
        ShapeElement shapeElement1 = new ShapeElement(getContext(), ResourceTable.Graphic_shape_switch_s);
        ShapeElement shapeElement2 = new ShapeElement(getContext(), ResourceTable.Graphic_shape_rb_df);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, shapeElement1);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, shapeElement2);
        return stateElement;
    }

    private StateElement getTrackElement(int radius) {
        StateElement stateElement = new StateElement();
        ShapeElement shapeElement1 = new ShapeElement();
        shapeElement1.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#FF7F9F")));
        shapeElement1.setCornerRadius(radius);
        ShapeElement shapeElement2 = new ShapeElement();
        shapeElement2.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#999999")));
        shapeElement2.setCornerRadius(radius);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, shapeElement1);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, shapeElement2);
        return stateElement;
    }

    void setUpRadioButtonsAndSwitches() {

        RadioContainer radioGroup1 = (RadioContainer) findComponentById(ResourceTable.Id_radioGroup1);
        radioGroup1.setMarkChangedListener(this::onRadioContainerCheckedChanged);
        RadioButton stacking1 = (RadioButton) findComponentById(ResourceTable.Id_stacking1);
        stacking1.setButtonElement(getButtonElement());
        RadioButton stacking2 = (RadioButton) findComponentById(ResourceTable.Id_stacking2);
        stacking2.setButtonElement(getButtonElement());
        RadioButton stacking3 = (RadioButton) findComponentById(ResourceTable.Id_stacking3);
        stacking3.setButtonElement(getButtonElement());

        RadioContainer radioGroup2 = (RadioContainer) findComponentById(ResourceTable.Id_radioGroup2);
        radioGroup2.setMarkChangedListener(this::onRadioContainerCheckedChanged);
        RadioButton symbol1 = (RadioButton) findComponentById(ResourceTable.Id_symbol1);
        symbol1.setButtonElement(getButtonElement());
        RadioButton symbol2 = (RadioButton) findComponentById(ResourceTable.Id_symbol2);
        symbol2.setButtonElement(getButtonElement());
        RadioButton symbol3 = (RadioButton) findComponentById(ResourceTable.Id_symbol3);
        symbol3.setButtonElement(getButtonElement());
        RadioButton symbol4 = (RadioButton) findComponentById(ResourceTable.Id_symbol4);
        symbol4.setButtonElement(getButtonElement());
        RadioButton symbol5 = (RadioButton) findComponentById(ResourceTable.Id_symbol5);
        symbol5.setButtonElement(getButtonElement());

        Switch boolSwitch1 = (Switch) findComponentById(ResourceTable.Id_switch1);
        boolSwitch1.setCheckedStateChangedListener(this::onSwitchCheckedChanged);
        boolSwitch1.setThumbElement(getThumbElement());
        boolSwitch1.setTrackElement(getTrackElement(boolSwitch1.getHeight() / 2));


        Switch boolSwitch2 = (Switch) findComponentById(ResourceTable.Id_switch2);
        boolSwitch2.setCheckedStateChangedListener(this::onSwitchCheckedChanged);
        boolSwitch2.setThumbElement(getThumbElement());
        boolSwitch2.setTrackElement(getTrackElement(boolSwitch2.getHeight() / 2));


        Switch boolSwitch3 = (Switch) findComponentById(ResourceTable.Id_switch3);
        boolSwitch3.setCheckedStateChangedListener(this::onSwitchCheckedChanged);
        boolSwitch3.setThumbElement(getThumbElement());
        boolSwitch3.setTrackElement(getTrackElement(boolSwitch3.getHeight() / 2));

        Switch boolSwitch4 = (Switch) findComponentById(ResourceTable.Id_switch4);
        boolSwitch4.setCheckedStateChangedListener(this::onSwitchCheckedChanged);
        boolSwitch4.setThumbElement(getThumbElement());
        boolSwitch4.setTrackElement(getTrackElement(boolSwitch4.getHeight() / 2));

        Switch boolSwitch5 = (Switch) findComponentById(ResourceTable.Id_switch5);
        boolSwitch5.setCheckedStateChangedListener(this::onSwitchCheckedChanged);
        boolSwitch5.setThumbElement(getThumbElement());
        boolSwitch5.setTrackElement(getTrackElement(boolSwitch5.getHeight() / 2));
    }


    @Override
    public void chartViewDidFinishLoad(AAChartView aaChartView) {
        System.out.println("🔥🔥🔥🔥🔥图表加载完成回调方法!!!!!!!! ");
    }

    @Override
    public void chartViewMoveOverEventMessage(AAChartView aaChartView, AAMoveOverEventMessageModel messageModel) {
        Gson gson = new Gson();
        System.out.println("👌👌👌👌👌move over event message " + gson.toJson(messageModel));
    }

    private void onRadioContainerCheckedChanged(RadioContainer group, int i) {
        if (group.getId() == ResourceTable.Id_radioGroup1) {
            //根据不同ID 弹出不同的吐司
            switch (group.getComponentAt(i).getId()) {
                case ResourceTable.Id_stacking1:
                    aaChartModel.stacking(AAChartStackingType.False);
                    break;
                case ResourceTable.Id_stacking2:
                    aaChartModel.stacking(AAChartStackingType.Normal);
                    break;
                case ResourceTable.Id_stacking3:
                    aaChartModel.stacking(AAChartStackingType.Percent);
                    break;
            }
        } else {
            switch (group.getComponentAt(i).getId()) {
                case ResourceTable.Id_symbol1:
                    aaChartModel.markerSymbol(AAChartSymbolType.Circle);
                    break;
                case ResourceTable.Id_symbol2:
                    aaChartModel.markerSymbol(AAChartSymbolType.Diamond);
                    break;
                case ResourceTable.Id_symbol3:
                    aaChartModel.markerSymbol(AAChartSymbolType.Square);
                    break;
                case ResourceTable.Id_symbol4:
                    aaChartModel.markerSymbol(AAChartSymbolType.Triangle);
                    break;
                case ResourceTable.Id_symbol5:
                    aaChartModel.markerSymbol(AAChartSymbolType.Triangle_down);
                    break;
            }
        }

        aaChartView.aa_refreshChartWithChartModel(aaChartModel);
    }

    private void onSwitchCheckedChanged(AbsButton absButton, boolean isChecked) {
        switch (absButton.getId()) {
            case ResourceTable.Id_switch1:
                aaChartModel.xAxisReversed(isChecked);
                break;
            case ResourceTable.Id_switch2:
                aaChartModel.yAxisReversed(isChecked);
                break;
            case ResourceTable.Id_switch3:
                aaChartModel.inverted(isChecked);
                break;
            case ResourceTable.Id_switch4:
                aaChartModel.polar(isChecked);
                break;
            case ResourceTable.Id_switch5:
                aaChartModel.dataLabelsEnabled(isChecked);
                break;
        }

        aaChartView.aa_refreshChartWithChartModel(aaChartModel);
    }
}
